package com.zzs.team.Controller;

import com.google.gson.Gson;
import com.sun.scenario.effect.impl.state.LinearConvolveKernel;
import com.zzs.team.Entity.Type;
import com.zzs.team.Service.LocationService;
import com.zzs.team.Service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Controller
public class TypeController {
    @Autowired
    TypeService typeService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @ResponseBody
    @GetMapping("/type/getTypes")
    public Map getTypes(){
        Map map = new HashMap();
        Gson gson = new Gson();
        List<String> types = new LinkedList<>();
        String typeStr = stringRedisTemplate.opsForValue().get("types");

        if(typeStr==null){
            System.out.println("类型redis中没有，从数据库中拿");
            types =typeService.findAllTypes() ;
            types.add(0,"请选择组队类型");
            typeStr  = gson.toJson(types);
            stringRedisTemplate.opsForValue().set("types",typeStr);
        }else{
            System.out.println("从redis中取类型");
            types = gson.fromJson(typeStr,List.class);
        }

        System.out.println(types);
        map.put("types",types);
        return map;
    }
}
